<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-04 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'servlet/module/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		require_once ROOT_PATH.'include/logic/image.logic.php';
		
		$this->excute();
	}
	
	function excute()
	{
		switch($this->act)
		{
			case 'list':
				$this->getReviewReplyList();
				break;
			case 'add_reply':
				$this->addReply();
				break;
			case 'vote':
				$this->addVote();
				break;
			default:
				$this->message('004', STATUS_E);
		}
		
		$this->showResponse();
	}
	
	/**
	 * 
	 * Add a reply to movie review
	 */
	private function addReply()
	{		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
				
		$request['review_id'] = (int)$this->request['rid'];
		$request['user_id'] = $this->user['id'];
		$request['user_addr'] = $this->request['addr'];
		$request['content'] = $this->request['content'];
		
		$results = get_movie_review_detail($request['review_id']);
		
		if(!check_array($results))
		{
			$this->message('005', STATUS_E);
		}
		if(!$request['content'])
		{
			$this->message('007', STATUS_E);
		}
		
		$request['movie_id'] = $results['movie_id'];
		
		$results = add_movie_review_reply($request);
				
		if($results)
		{
			$this->message('008', STATUS_S);
		}
		else
		{
			$this->message('009', STATUS_F);
		}
	}
	
	/**
	 * 
	 * Add a vote to movie review reply
	 */
	private function addVote()
	{
		// TODO, user should only vote once ?
		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
				
		$request['reply_id'] = (int)$this->request['rid'];
		$request['vote'] = $this->request['vote'];
		
		$results = get_movie_review_reply($request['reply_id']);
		
		if(!check_array($results))
		{
			$this->message('005', STATUS_E);
		}
		if(!$request['vote'] || !in_array($request['vote'], array('up', 'down')))
		{
			$this->message('010', STATUS_E);
		}
		
		$results = add_movie_review_reply_vote($request);
				
		if($results)
		{
			$this->message('008', STATUS_S);
		}
		else
		{
			$this->message('009', STATUS_F);
		}
	}
	
	/**
	 * 
	 * Get reply list of movie review
	 */
	private function getReviewReplyList()
	{		
		if(!isset($this->request['rid']) || !$this->request['rid'])
		{
			$this->message('005', STATUS_E);
		}
		
		$request['from_id'] = $this->from_id;
		$request['to_id'] = $this->to_id;
		$request['page_limit'] = $this->page_limit;
		$request['page_num'] = $this->page_num;
		$request['review_id'] = (int)$this->request['rid'];
		
		$results = get_movie_review_reply_list($request);
		
		$reply_list = array();
		
		if(check_array($results))
		{
			require ROOT_PATH.'servlet/include/output/movie_review_reply.php';
			foreach($results as $item)
			{
				$item['face'] = make_image($item['user_id'], 'face/', BASE_URL, 's');
				if(!$item['face']) $item['face'] = BASE_URL.'templates/default/images/face_blank.gif';
				$reply_list[] = new movie_review_reply($item);
			}
		}
		
		$this->response['status'] = STATUS_S;
		$this->response['count'] = (string)count($reply_list);
		$this->response['list'] = $reply_list;
	}
}